<template>
  <div>
    <!-- 添加或修改产品对话框 -->
    <el-dialog title="碳核算" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" custom-class="show-back-class" >
      <div style="height: 600px">
        <div style="height: 98%;width: 30%;float: left" >
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary"  icon="el-icon-plus" size="mini" @click="handleShowCheckItemOpen" >选择导入</el-button>
            </el-col>
          </el-row>
          <el-scrollbar class="stdUnitScrollbarDiv" style="height: 100%;background-color:#07314E;" v-if="menuList.length>0">
            <el-table  ref="menuTreeTableRef" :data="menuList" :show-header="false" default-expand-all
                        row-key="id" highlight-current-row
                        @row-click="clickMenu" row-class-name="treeTableRow"
                        :tree-props="{children: 'children', hasChildren: 'hasChildren'}" >
              <el-table-column label="名称" align="left" prop="name" >
                <template slot-scope="scope">
                  <i v-if="scope.row.type===1" class="el-icon-location"></i>
                  <span >{{ scope.row.name}}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-scrollbar>
        </div>
        <div style="height: 100%;width: 70%;;float: left">
          <product-consumables ref="productConsumablesRef" :product-id="form.productId" v-if="form.productId"/>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: 40px">
        <el-button type="primary" @click="submitForm">核 算</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="选择导入其他批次参数" :visible.sync="checkItemOpen" width="700px" append-to-body custom-class="show-back-class" >
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="所属企业" prop="enterpriseId">
          <el-cascader  v-model="enterpriseIdArr" style="width: 100%"
                        :options="$store.state.factorDict.enterpriseDict" :show-all-levels="false"
                        :props="{value:'id',label:'enterpriseName',children:'children',checkStrictly:true,}"
                        :clearable="false"></el-cascader>
        </el-form-item>
        <el-form-item label="产品批次" prop="productBatch">
          <el-input  v-model="queryParams.productBatch" placeholder="请输入产品批次" clearable   />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table border   :data="productCarbonList" height="400px">
        <el-table-column label="序号" align="center" type="index" width="50" />
        <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
        <el-table-column label="所属企业" align="center" prop="enterpriseId"  :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{selectDictLabel($store.state.factorDict.enterpriseOptions, scope.row.enterpriseId,'id','enterpriseName')}}
          </template>
        </el-table-column>
        <el-table-column label="产品批次" align="center" prop="productBatch"  :show-overflow-tooltip="true"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="55">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-download" v-if="scope.row.id!=form.id"
              @click="handleDownload(scope.row)" >导入</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="handleQuery"
      />
      <div slot="footer" class="dialog-footer">
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import { listProductCarbon, getProductCarbon, delProductCarbon, addProductCarbon, calCarbonQuantityById, downloadProductCarbon } from "@/api/carbon/productCarbon";
import ProductConsumables from './ProductCarbonConsumables'
export default {
  name: "ProductCarbonStageForm",
  components:{
    ProductConsumables
  },
  data() {
    return {
      enterpriseIdArr:[],
      open: false,
      checkItemOpen: false,
      form: {},
      menuList:[],
      openeds:[],


      productCarbonList:[],
      queryParams:{
        productBatch:null,
        productId: null,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      productCarbonId: null,
    };
  },
  created() {
  },
  methods: {
    handleShowCheckItemOpen(){
      this.enterpriseIdArr=[];
      this.queryParams.productBatch=null;
      this.productCarbonList = [];
      this.total = 0;
      this.checkItemOpen=true
    },
    handleQuery(){
      // if(this.enterpriseIdArr.length>0){
      //   this.queryParams.enterpriseId = this.enterpriseIdArr[this.enterpriseIdArr.length-1];
      // }else {
      //   this.queryParams.enterpriseId = null;
      // }
      // this.queryParams.productId = this.form.productId;
      listProductCarbon(this.queryParams).then(response => {
        this.productCarbonList = response.rows;
        this.total = response.total;
      });
    },
    handleDownload(row){
      let data = {id:this.form.id,productId:row.id};
      let that = this;
      this.$confirm('是否确认导入【'+row.productBatch+'】的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return downloadProductCarbon(data);
      }).then(() => {
        this.handleShow(that.form);
        this.checkItemOpen=false;
        this.msgSuccess("导入成功");
      }).catch(function() {});
    },


    clickMenu(row, column, event){
      this.$refs.productConsumablesRef.checkStageItem(row);
    },
    handleShow(row) {
      this.reset();
      this.productCarbonId=row.id;
      getProductCarbon(row.id).then(response => {
        this.form = response.data;
        if(response.data.stageList){
            this.openeds = response.data.stageList.filter(item => item.parentId===0).map((item) => "I_"+item.id);
          this.menuList =  this.handleTree(response.data.stageList, "id", "parentId");
        }else {
          this.menuList=[];
          this.openeds=[]
        }
        if( this.$refs.productConsumablesRef){
          this.$refs.productConsumablesRef.selectAvgFun(this.form.id);
        }
        this.open = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        status: null,
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        stageList:[],
      };
      this.resetForm("form");
      this.menuList=[];
      this.productCarbonId=null;
    },
    submitForm(){
      calCarbonQuantityById(this.productCarbonId).then(response => {
        if (response.code === '200') {
          this.msgSuccess("核算成功");
          this.open = false;
          this.$emit("getList")
        }
      });
    }


  }
};
</script>
<!-- <style lang="scss">

  .treeTableRow{
    background-color: #545c64 !important;
    color: #fff !important;
   .el-icon-arrow-right{
     color: #fff !important;
   }
    td{
      border-bottom:0 solid #dfe6ec  !important;
    }
  }

  .treeTableRow.current-row{
    color: rgb(255, 208, 75) !important;
    td{
      background-color: #545c64 !important;
    }
  }
  .treeTableRow:hover{
    background-color: #3d4349 !important;
    td{
      background-color: #3d4349 !important;
    }
  }

</style> -->
<style lang="scss" scoped>
.el-dialog__footer {
  // padding: auto;
  border-top: 0px !important;
}
</style>
<style lang="scss">

  .treeTableRow{
    background-color: #092038 !important;
    color: #fff !important;
   .el-icon-arrow-right{
     color: #fff !important;
   }
    td{
      border-bottom:0 solid #dfe6ec  !important;
    }
  }

  .treeTableRow.current-row{
    color: #FEE034 !important;
    td{
      background-color: #545c64 !important;
    }
  }
  .treeTableRow:hover{
    background-color: #3d4349 !important;
    td{
      // background-color: #3d4349 !important;
      background-color: #2496c9 !important;
    }
  }

</style>